﻿using System;
using System.Collections.Generic;
using System.Linq;
using TaleWorlds.CampaignSystem.Actions;
using TaleWorlds.CampaignSystem.Party;
using TaleWorlds.CampaignSystem.Settlements;
using TaleWorlds.Core;
using TaleWorlds.Library;
using TaleWorlds.SaveSystem;

namespace TaleWorlds.CampaignSystem.TournamentGames
{
	// Token: 0x02000279 RID: 633
	public class TournamentManager : ITournamentManager
	{
		// Token: 0x060021C5 RID: 8645 RVA: 0x00090015 File Offset: 0x0008E215
		internal static void AutoGeneratedStaticCollectObjectsTournamentManager(object o, List<object> collectedObjects)
		{
			((TournamentManager)o).AutoGeneratedInstanceCollectObjects(collectedObjects);
		}

		// Token: 0x060021C6 RID: 8646 RVA: 0x00090023 File Offset: 0x0008E223
		protected virtual void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
		{
			collectedObjects.Add(this._activeTournaments);
			collectedObjects.Add(this._worldWideTournamentLeaderboard);
		}

		// Token: 0x060021C7 RID: 8647 RVA: 0x0009003D File Offset: 0x0008E23D
		internal static object AutoGeneratedGetMemberValue_activeTournaments(object o)
		{
			return ((TournamentManager)o)._activeTournaments;
		}

		// Token: 0x060021C8 RID: 8648 RVA: 0x0009004A File Offset: 0x0008E24A
		internal static object AutoGeneratedGetMemberValue_worldWideTournamentLeaderboard(object o)
		{
			return ((TournamentManager)o)._worldWideTournamentLeaderboard;
		}

		// Token: 0x060021C9 RID: 8649 RVA: 0x00090057 File Offset: 0x0008E257
		public TournamentManager()
		{
			this._worldWideTournamentLeaderboard = new Dictionary<Hero, int>();
			this._activeTournaments = new List<TournamentGame>();
		}

		// Token: 0x060021CA RID: 8650 RVA: 0x00090075 File Offset: 0x0008E275
		public void AddTournament(TournamentGame game)
		{
			this._activeTournaments.Add(game);
			CampaignEventDispatcher.Instance.OnTournamentStarted(game.Town);
		}

		// Token: 0x060021CB RID: 8651 RVA: 0x00090093 File Offset: 0x0008E293
		public void RemoveTournament(TournamentGame game)
		{
			this._activeTournaments.Remove(game);
		}

		// Token: 0x060021CC RID: 8652 RVA: 0x000900A4 File Offset: 0x0008E2A4
		public TournamentGame GetTournamentGame(Town town)
		{
			return this._activeTournaments.Find((TournamentGame x) => x != null && x.Town == town);
		}

		// Token: 0x060021CD RID: 8653 RVA: 0x000900D5 File Offset: 0x0008E2D5
		public void OnPlayerJoinMatch(Type gameType)
		{
		}

		// Token: 0x060021CE RID: 8654 RVA: 0x000900D8 File Offset: 0x0008E2D8
		public void OnPlayerJoinTournament(Type gameType, Settlement settlement)
		{
			CampaignEventDispatcher.Instance.OnPlayerJoinedTournament(settlement.Town, true);
			TournamentGame tournamentGame = this.GetTournamentGame(settlement.Town);
			this._activeTournaments.Remove(tournamentGame);
		}

		// Token: 0x060021CF RID: 8655 RVA: 0x00090110 File Offset: 0x0008E310
		public void OnPlayerWatchTournament(Type gameType, Settlement settlement)
		{
			CampaignEventDispatcher.Instance.OnPlayerJoinedTournament(settlement.Town, false);
			TournamentGame tournamentGame = this.GetTournamentGame(settlement.Town);
			this._activeTournaments.Remove(tournamentGame);
		}

		// Token: 0x060021D0 RID: 8656 RVA: 0x00090148 File Offset: 0x0008E348
		public void OnPlayerWinMatch(Type gameType)
		{
		}

		// Token: 0x060021D1 RID: 8657 RVA: 0x0009014A File Offset: 0x0008E34A
		public void OnPlayerWinTournament(Type gameType)
		{
		}

		// Token: 0x060021D2 RID: 8658 RVA: 0x0009014C File Offset: 0x0008E34C
		public List<KeyValuePair<Hero, int>> GetLeaderboard()
		{
			return this._worldWideTournamentLeaderboard.OrderByDescending(delegate(KeyValuePair<Hero, int> pair)
			{
				KeyValuePair<Hero, int> keyValuePair = pair;
				return keyValuePair.Value;
			}).ToList<KeyValuePair<Hero, int>>();
		}

		// Token: 0x060021D3 RID: 8659 RVA: 0x00090180 File Offset: 0x0008E380
		public int GetLeaderBoardRank(Hero hero)
		{
			return this.GetLeaderboard().FindIndex((KeyValuePair<Hero, int> kvp) => kvp.Key == hero) + 1;
		}

		// Token: 0x060021D4 RID: 8660 RVA: 0x000901B4 File Offset: 0x0008E3B4
		public Hero GetLeaderBoardLeader()
		{
			Dictionary<Hero, int> worldWideTournamentLeaderboard = this._worldWideTournamentLeaderboard;
			Hero result = null;
			int num = -1;
			foreach (Hero hero in worldWideTournamentLeaderboard.Keys)
			{
				int num2 = worldWideTournamentLeaderboard[hero];
				if (num2 > num)
				{
					num = num2;
					result = hero;
				}
			}
			return result;
		}

		// Token: 0x060021D5 RID: 8661 RVA: 0x00090224 File Offset: 0x0008E424
		public void InitializeLeaderboardEntry(Hero hero, int initialVictories = 0)
		{
			this._worldWideTournamentLeaderboard[hero] = initialVictories;
		}

		// Token: 0x060021D6 RID: 8662 RVA: 0x00090234 File Offset: 0x0008E434
		public void AddLeaderboardEntry(Hero hero)
		{
			int num;
			this._worldWideTournamentLeaderboard.TryGetValue(hero, out num);
			this._worldWideTournamentLeaderboard[hero] = num + 1;
		}

		// Token: 0x060021D7 RID: 8663 RVA: 0x0009025F File Offset: 0x0008E45F
		public void DeleteLeaderboardEntry(Hero hero)
		{
			if (this._worldWideTournamentLeaderboard.ContainsKey(hero))
			{
				this._worldWideTournamentLeaderboard.Remove(hero);
			}
		}

		// Token: 0x060021D8 RID: 8664 RVA: 0x0009027C File Offset: 0x0008E47C
		public void ResolveTournament(TournamentGame tournament, Town town)
		{
			if (!town.IsUnderSiege)
			{
				MBList<CharacterObject> participantCharacters = tournament.GetParticipantCharacters(tournament.Town.Settlement, false);
				CharacterObject characterObject;
				this.SimulateTournament(participantCharacters, tournament.Town, out characterObject);
				if (characterObject.IsHero)
				{
					this.AddLeaderboardEntry(characterObject.HeroObject);
					this.GivePrizeToWinner(tournament, characterObject.HeroObject, false);
				}
				CampaignEventDispatcher.Instance.OnTournamentFinished(characterObject, participantCharacters, tournament.Town, tournament.Prize);
			}
			else
			{
				CampaignEventDispatcher.Instance.OnTournamentCancelled(town);
			}
			this.RemoveTournament(tournament);
		}

		// Token: 0x060021D9 RID: 8665 RVA: 0x00090304 File Offset: 0x0008E504
		private void SimulateTournament(MBList<CharacterObject> participants, Town town, out CharacterObject winner)
		{
			float num = -1f;
			winner = null;
			foreach (CharacterObject characterObject in participants)
			{
				if (characterObject.IsHero)
				{
					ValueTuple<SkillObject, int> skillXpGainFromTournament = Campaign.Current.Models.TournamentModel.GetSkillXpGainFromTournament(town);
					SkillObject item = skillXpGainFromTournament.Item1;
					int item2 = skillXpGainFromTournament.Item2;
					characterObject.HeroObject.HeroDeveloper.AddSkillXp(item, (float)item2, true, true);
				}
				float num2 = Campaign.Current.Models.TournamentModel.GetTournamentSimulationScore(characterObject) * (0.75f + 0.25f * MBRandom.RandomFloat);
				if (num2 > num)
				{
					winner = characterObject;
					num = num2;
				}
			}
		}

		// Token: 0x060021DA RID: 8666 RVA: 0x000903CC File Offset: 0x0008E5CC
		public void GivePrizeToWinner(TournamentGame tournament, Hero winner, bool isPlayerParticipated)
		{
			if (!isPlayerParticipated)
			{
				tournament.UpdateTournamentPrize(isPlayerParticipated, false);
			}
			if (winner.PartyBelongedTo == MobileParty.MainParty)
			{
				winner.PartyBelongedTo.ItemRoster.AddToCounts(tournament.Prize, 1);
				return;
			}
			if (winner.Clan != null)
			{
				GiveGoldAction.ApplyBetweenCharacters(null, winner.Clan.Leader, tournament.Town.MarketData.GetPrice(tournament.Prize, null, false, null), false);
			}
		}

		// Token: 0x04000A73 RID: 2675
		[SaveableField(1)]
		private List<TournamentGame> _activeTournaments;

		// Token: 0x04000A74 RID: 2676
		[SaveableField(4)]
		private readonly Dictionary<Hero, int> _worldWideTournamentLeaderboard;
	}
}
